//入口函数
$(function () {
    //渲染文章分类列表
    initArtCateList();

    let layer = layui.layer;
    //封装函数
    function initArtCateList() {
        $.ajax({
            url: '/my/article/cates',
            success: function (res) {
                // console.log(res);

                //判断状态码
                if (res.status != 0) {
                    return layer.msg(res.message)
                }
                let str = template('tpl-art-cate', res);
                $('tbody').html(str);
            }
        })
    }

    //添加类别对话框

    $("#btnAdd").on('click', function () {
        indexAdd = layer.open({
            type: 1,
            title: '添加文章分类',
            area: ['500px', '260px'],
            content: $("#dialog-add").html()
        })
    })


    // 添加文章分类
    let indexAdd = null;
    $('body').on('submit', '#form-add', function (e) {
        e.preventDefault();
        $.ajax({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize(),
            success: function (res) {
                if (res.status != 0) {
                    return layer.msg(res.message);
                }
                layer.msg('添加成功');
                initArtCateList();
                layer.close(indexAdd);
            }
        })
    });

   



 




    // 修改  展示表单
    let indexEdit = null;
    let form = layui.form;
    $('tbody').on('click', '.btn-edit', function () {
        indexEdit = layer.open({
            type: 1,
            title: '修改文章分类',
            area: ['500px', '260px'],
            content: $('#dialog-edit').html()
        });
        let Id = $(this).attr('data-id');
        $.ajax({
            method: 'GET',
            url: '/my/article/cates/' + Id,
            success: function (res) {
                form.val('form-edit', res.data)
            }
        })
        
    });

    //修改  提交
    $('body').on('submit', '#form-edit', function (e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize(),
            success: function (res) {
                if (res.status != 0) {
                    return layer.msg(res.message)
                }
                initArtCateList();
                layer.msg('修改成功');
                layer.close(indexEdit);
            }
        })
    })

    // 删除
    $('tbody').on('click', '.btn-delete', function () {
        let Id = $(this).attr('data-id');
        console.log(123);
        layer.confirm('是否确认删除?', { icon: 3, title: '提示' },
            function (index) {
                $.ajax({
                    method: 'GET',
                    url: '/my/article/deletecate/' + Id,
                    success: function (res) {
                        if (res.status != 0) {
                            return layer.msg(res.message)
                        }
                        initArtCateList();
                        layer.msg('删除成功');
                        layer.close(index);
                    }
                })
            });

    });

});






